package com.sean.controller;


import com.sean.entity.Student;
import com.sean.service.StudentService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/login")
    public String login(@RequestBody Student student){


        //master branch remote change
        //master local change

        studentService.createStudent(student);
//
        return "user";

    }
    @RequestMapping("/login_two")
    public String login2(){
        return "user_two";
    }

    @RequestMapping("/interceptor")
    public String interCeptor(){
        return "user_two";
    }

    @RequestMapping("/upload")
    public String upLoad(MultipartFile file) throws IOException {
        String fileName=file.getOriginalFilename();
        if(fileName.endsWith("txt")||fileName.endsWith("jpg")){
            String filePath="D:/"+ fileName;
            File newFile=new File(filePath);
            file.transferTo(newFile);

        }

        return "user_two";

    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download() throws IOException {
        String path="D:\\1.txt";
        File file=new File(path);  //已经打包好 待下载的文件

        String filename="docu.txt";
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment",filename);  //设置要下载的文件名
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);   //设置文件格式

        return  new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),httpHeaders,HttpStatus.CREATED);
    }

    @RequestMapping("/tt")
    public Object code190527(){
        System.out.println("something");
        return null;
    }

}
